package com.logos.ruralrevitalization.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.logos.ruralrevitalization.entity.AgriculturalProduct;
import com.logos.ruralrevitalization.req.AgriculturalProductReq;
import com.logos.ruralrevitalization.resp.AgriculturalProductResp;
import com.logos.ruralrevitalization.result.Result;
import com.logos.ruralrevitalization.service.AgriculturalProductService;
import com.logos.ruralrevitalization.utils.RuralLoginUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 农产品表(AgriculturalProduct)表控制层
 *
 * @author rose
 * @since 2025-03-25 12:27:49
 */
@RestController
@RequestMapping("/agriculturalProduct")
@Slf4j
public class AgriculturalProductController {

    @Resource
    private AgriculturalProductService agriculturalProductService;

    @Resource
    private RuralLoginUtil ruralLoginUtil;


    /**
     * 查询农产品详情
     */
    @PostMapping("/queryById")
    public Result<AgriculturalProductResp> queryById(@RequestBody AgriculturalProductReq agriculturalProductReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AgriculturalProductController.queryById.agriculturalProductReq:{}", JSON.toJSONString(agriculturalProductReq));
            }
            Preconditions.checkNotNull(agriculturalProductReq, "农产平id不能为空");
            AgriculturalProductResp agriculturalProduct = this.agriculturalProductService.queryById(agriculturalProductReq.getId());
            return Result.ok(agriculturalProduct);
        } catch (Exception e) {
            log.error("AgriculturalProductController.queryById.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 根据产品类型查询农产品,包括销量优先与价格优先
     */
    @PostMapping("/queryByProductType")
    public Result<List<AgriculturalProductResp>> queryByProductType(@RequestBody AgriculturalProductReq agriculturalProductReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AgriculturalProductController.queryByProductType.agriculturalProductReq:{}", JSON.toJSONString(agriculturalProductReq));
            }
            Preconditions.checkNotNull(agriculturalProductReq.getPageNo(), "页码不能为空");
            Preconditions.checkNotNull(agriculturalProductReq.getPageSize(), "每页条数不能为空");
            List<AgriculturalProductResp> result = agriculturalProductService.queryByProductType(agriculturalProductReq);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("AgriculturalProductController.queryByProductType:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询热销农产品排行
     */
    @GetMapping("/queryHotRank")
    public Result<List<AgriculturalProductResp>> queryHotSales() {
        try {
            List<AgriculturalProductResp> result = agriculturalProductService.queryHotSales();
            return Result.ok(result);
        } catch (Exception e) {
            log.error("AgriculturalProductController.queryHotSales:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询单个农产品详情
     */
    @GetMapping("/{id}")
    public Result<AgriculturalProductResp> queryById(@PathVariable("id") Long id) {
        try {
            log.info("AgriculturalProductController.queryById.id:{}", id);
            AgriculturalProductResp result = agriculturalProductService.queryById(id);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("AgriculturalProductController.queryById:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 搜索农产品
     */
    @PostMapping("/search")
    public Result<List<AgriculturalProductResp>> search(@RequestBody AgriculturalProductReq agriculturalProductReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AgriculturalProductController.search.agriculturalProductReq:{}", JSON.toJSONString(agriculturalProductReq));
            }
            Preconditions.checkNotNull(agriculturalProductReq.getSearchKey(), "搜索关键字不能为空");
            List<AgriculturalProductResp> result = agriculturalProductService.search(agriculturalProductReq);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("AgriculturalProductController.search.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询当前农户上架的农产品
     */
    @GetMapping("/queryRuralProd")
    public Result<List<AgriculturalProductResp>> queryRuralProd() {
        try {
            String id = ruralLoginUtil.getId();
            if (StringUtils.isBlank(id)) {
                return Result.fail("请先登录");
            }
            log.info("AgriculturalProductController.queryRuralProd.id:{}", id);
            List<AgriculturalProductResp> result = agriculturalProductService.queryRuralProd(Long.valueOf(id));
            return Result.ok(result);
        } catch (Exception e) {
            log.error("AgriculturalProductController.queryRuralProd:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 上架农产品
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AgriculturalProductReq agriculturalProductReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AgriculturalProductController.add.agriculturalProductReq:{}", JSON.toJSONString(agriculturalProductReq));
            }
            Preconditions.checkNotNull(agriculturalProductReq.getName(), "农产品名称不能为空");
            Preconditions.checkNotNull(agriculturalProductReq.getDescription(), "农产品描述不能为空");
            Preconditions.checkNotNull(agriculturalProductReq.getProductType(), "农产品类型不能为空");
            Preconditions.checkNotNull(agriculturalProductReq.getPrice(), "农产品价格不能为空");
            Preconditions.checkNotNull(agriculturalProductReq.getProductionDate(), "农产品生产日期不能为空");
            Preconditions.checkNotNull(agriculturalProductReq.getExpirationDate(), "农产品过期日期不能为空");
            Preconditions.checkNotNull(agriculturalProductReq.getAvatar(), "农产品图片不能为空");
            String id = ruralLoginUtil.getId();
            if (StringUtils.isBlank(id)) {
                return Result.fail("请先登录");
            }
            agriculturalProductReq.setSupplierId(Long.valueOf(id));
            Boolean result = agriculturalProductService.add(agriculturalProductReq);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("AgriculturalProductController.add.error:{}", e.getMessage(), e);
            return Result.fail("上架农产品失败");
        }
    }


    /**
     * 下架农产品
     */
    @PostMapping("/down")
    public Result<Boolean> delete(@RequestBody AgriculturalProductReq agriculturalProductReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AgriculturalProductController.delete.agriculturalProductReq:{}", JSON.toJSONString(agriculturalProductReq));
            }
            Preconditions.checkNotNull(agriculturalProductReq.getId(), "农产品id不能为空");
            Boolean result = agriculturalProductService.down(agriculturalProductReq);
            return Result.ok(result);
        }catch(Exception e){
            log.error("AgriculturalProductController.delete.error:{}", e.getMessage(), e);
            return Result.fail("下架农产品失败");
        }
    }

    /**
     * 修改农产品
     */
    @PostMapping("/updateInfo")
    public Result<Boolean> updateInfo(@RequestBody AgriculturalProductReq agriculturalProductReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AgriculturalProductController.update.agriculturalProductReq:{}", JSON.toJSONString(agriculturalProductReq));
            }
            return Result.ok(agriculturalProductService.updateInfo(agriculturalProductReq));
        }catch (Exception e){
            log.error("AgriculturalProductController.update.error:{}", e.getMessage(), e);
            return Result.fail("修改农产品失败");
        }
    }


    /**
     * 首页展示特色农产品
     */
    @GetMapping("/showHome")
    public Result<List<AgriculturalProductResp>> showHome() {
        try {
            List<AgriculturalProductResp> result = agriculturalProductService.showHome();
            return Result.ok(result);
        } catch (Exception e) {
            log.error("AgriculturalProductController.showHome:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

}

